package com.liaozibo.demo.seckill.web.controller;

import com.liaozibo.demo.seckill.support.export.dto.ProductDTO;
import com.liaozibo.demo.seckill.support.export.dto.Result;
import com.liaozibo.demo.seckill.support.export.service.ProductExportService;
import com.liaozibo.demo.seckill.web.model.ProductDescDTO;
import com.liaozibo.demo.seckill.web.util.ResultUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 *  商品信息查询（不属于秒杀系统）
 * */
@RestController
@RequestMapping("/product")
public class ProductController {

    @DubboReference
    private ProductExportService productExportService;

    @GetMapping("/productData")
    public Result<ProductDescDTO> productApi(@RequestParam String code) {
        Result<ProductDTO> productDTOResult = productExportService.getProductByCode(code);
        if (ResultUtils.isNull(productDTOResult)) {
            return Result.fail();
        }
        ProductDTO productDTO = productDTOResult.getData();
        ProductDescDTO productDescDTO = new ProductDescDTO();
        BeanUtils.copyProperties(productDTO, productDescDTO);
        productDescDTO.setTag(productDTO.getTag() == ProductDTO.TAG_SECKILL_PRODUCT  ? "秒杀商品" : "普通商品");
        return Result.ok(productDescDTO);
    }

}
